<template>
  <div>
    <div class="appointment-management">

      <!-- 预约信息列表 -->
      <el-card class="appointment-card">
        <h3 slot="header">支付信息列表</h3>
        <el-table :data="orderData" style="width: 100%">
          <el-table-column prop="orderID" label="订单编号"></el-table-column>
          <el-table-column prop="name" label="顾客姓名" width="100px"></el-table-column>
          <el-table-column prop="room_id" label="房间号">
          </el-table-column>
          <el-table-column prop="room_type" label="房间类型" width="100px">
          </el-table-column>
          <el-table-column  label="预约时间">
            <template slot-scope="scope">{{ $fromatDate(scope.row.created_at) }}</template>
          </el-table-column>

          <el-table-column prop="total_price" label="总金额"></el-table-column>
          <el-table-column  label="支付状态">
            <template slot-scope="scope">
              <el-tag :type="statusColor(scope.row.orderState)">{{ statusText(scope.row.orderState) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="warning" size="small" @click="payAppointment(scope.row)" v-if="scope.row.orderState===2">未住退款</el-button>
              <el-button type="primary" size="small" @click="payAppointment(scope.row)" v-else>支付</el-button>

              <el-popconfirm
              confirm-button-text='确认'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除支付信息吗？"
              @confirm="deleteAppointment(scope.row)"
              >
              <el-button type="danger" size="small"  slot="reference">删除</el-button>
            </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
        v-model="page"
        :total="total"
        :page-size="pageSize"
        background
        small
        @current-change="handlePageChange"
      ></el-pagination>
      </el-card>

      <!-- 编辑预约信息 -->
      <el-dialog :visible.sync="editDialogVisible" title="支付二维码">
        <PayCode :payInfo="currentpayOrder" :key="currentpayOrder.id"/>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveAppointment">确定</el-button>
        </div>
      </el-dialog>

    </div>
  </div>
</template>

<script>

import { getReservationAPI, delReservationAPI } from '@/api/reservation'
export default {
  name: 'PayManage',
  components: { PayCode },
  data () {
    return {
      orderData: [], // 所有订单数据
      page: 1,
      pageSize: 5,
      total: 0,
      appointments: [
        { id: '001', name: '张三', date: '2023-04-15 10:00', type: '美甲' },
        { id: '002', name: '李四', date: '2023-04-16 14:00', type: '理发' },
        { id: '003', name: '王五', date: '2023-04-17 16:00', type: 'SPA' }
      ],
      newAppointment: {
        name: '',
        date: '',
        type: ''
      },
      editDialogVisible: false,
      deleteDialogVisible: false,
      currentpayOrder: {}
    }
  },

  mounted () {
    this.initOrderData()
  },

  methods: {
    // 初始化订单信息数据
    async initOrderData () {
      const page = this.page
      const pageSize = this.pageSize
      const { data: res } = await getReservationAPI({ page, pageSize })
      // console.log(res)
      this.orderData = res.data
      this.total = res.total
    },
    // 渲染支付状态
    statusText (status) {
      switch (status) {
        case 0:
          return '未支付'
        case 1:
          return '待支付'
        case 2:
          return '已支付'
        default:
          return '未知状态'
      }
    },
    statusColor (status) {
      switch (status) {
        case 0:
          return 'danger' // 红色
        case 1:
          return 'warning' // 黄色
        case 2:
          return 'success' // 绿色
        default:
          return 'info' // 蓝色
      }
    },

    payAppointment (obj) {
      this.currentpayOrder = obj
      // console.log(this.currentpayOrder)
      // console.log(obj.orderState)
      this.editDialogVisible = true
      // console.log('父组件', this.currentpayOrder)
    },
    saveAppointment () {
      this.editDialogVisible = false
      this.$message.success('支付成功！')
      this.initOrderData()
    },
    async deleteAppointment (obj) {
      // 通过订单编号删除
      const { data: res } = await delReservationAPI(obj.orderID)
      if (res.code === 200) {
        this.$message.success(res.msg)
        this.initOrderData()
      } else {
        this.$message.error(res.msg)
      }
    },

    // 分页器页码改变重新获取数据
    handlePageChange (page) {
      this.page = page
      this.initOrderData()
    }
  }
}
</script>

<style lang="less" scoped>

</style>
